package com.ly.sy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.sy.entity.*;
import com.ly.sy.mapper.CourseMapper;
import com.ly.sy.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.sy.util.getPlayAuthorurl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    //下面这两个依赖注入是为了方便课程删除时，连同他的视频和作业信息一起删除
    @Autowired
    private VideoService videoService;
    @Autowired
    private HomeworkService homeworkService;
    @Autowired
    private XktableService xktableService;
    private HttpSession session;
    @Override
    public Course selectcourseById(Course course) {
        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",course.getCourseId());
        Course CourseFormSql= getOne(queryWrapper);
        return CourseFormSql;
    }

    @Override
    public List<Course> courselikeLise(int page,String likestr,String strtwo) {
        if(page==0 && (strtwo=="" || strtwo==null)){
            QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
            queryWrapper.like("course_name",likestr);
            List<Course> CourseForSql = list(queryWrapper);
            return CourseForSql;
        }

        Page<Course> coursePage = new Page<>(page,15);

        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        if(page!=0 && (strtwo=="" || strtwo==null)){
            queryWrapper.like("course_name", likestr);
            page(coursePage,queryWrapper);
            long total = coursePage.getTotal();

            List<Course> recordes = coursePage.getRecords();
            return recordes;
        }else {
            //用于页面固定分类显示，当搜索内容可能出现如L和l这种大小写时用
            queryWrapper.like("course_name",likestr).or().like("course_name",strtwo);
            page(coursePage,queryWrapper);
            long total = coursePage.getTotal();

            List<Course> recordes = coursePage.getRecords();
            return recordes;
        }

    }


    //删除课程下的作业
    @Override
    public boolean deleteHomeworkBycourseId(Integer courseId) {

        QueryWrapper<Homework> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<Homework> homeworkList = homeworkService.list(queryWrapper1);
        if (!ObjectUtils.isEmpty(homeworkList)){
            QueryWrapper<Homework> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("course_id",courseId);
            System.out.println("==========================作业删除成功=======================");
            return homeworkService.remove(queryWrapper);
        }else return false;
    }
    //删除课程下的视频
    @Override
    public boolean deletevideoBycourseId(Integer courseId) {
        /**
         *
         *删除阿里点播云端数据
         * **/
        QueryWrapper<Video> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<Video> videoList = videoService.list(queryWrapper1);

        if (!ObjectUtils.isEmpty(videoList)){
            List<String> vi_url = new ArrayList<>();
            for(Video v: videoList){
                vi_url.add(v.getVideoUrl());
            }
            getPlayAuthorurl.deteleVideo(vi_url,null);
            System.out.println("============================阿里数据删除成功============================");
            //删除数据库数据
            QueryWrapper<Video> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("course_id",courseId);
            System.out.println("===============================视频删除成功==================================");
            return videoService.remove(queryWrapper);
        }else return false;


    }

    @Override
    public boolean deletexkBycourseId(Integer courseId) {
        QueryWrapper<Xktable> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("course_id",courseId);
        List<Xktable> xktableList = xktableService.list(queryWrapper1);
        if (!ObjectUtils.isEmpty(xktableList)){
            QueryWrapper<Xktable> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("course_id",courseId);
            System.out.println("==========================选课信息删除成功=======================");
            return xktableService.remove(queryWrapper);
        }else return false;
    }


}
